package com.meilai.project.controller.mini;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.business.workbench.FillReasonsForNotCooperatingDTO;
import com.meilai.project.dto.business.workbench.FillWeekCustomerTargetDTO;
import com.meilai.project.dto.business.workbench.SigningTimeDTO;
import com.meilai.project.dto.business.workbench.VisitRecordDTO;
import com.meilai.project.service.business.workbench.*;
import com.meilai.project.vo.business.workbench.CompanyYearTargetSummaryVO;
import com.meilai.project.vo.business.workbench.SignatureHistoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/mini/saleMonth")
@Api(tags = "小程序-工作日志")
public class MiniSaleMonthController {

    @Autowired
    private SaleMonthService service;

    @Autowired
    private YearWeekService yearWeekService;

    @Autowired
    private CompanyYearService companyYearService;

    @Autowired
    private SaleYearTargetService saleYearTargetService;

    @Autowired
    private CompanyMonthService companyMonthService;

    @ApiOperation("获取销售半年目标汇总")
    @GetMapping("/querySaleYearTargetSummary")
    public CommonResult querySaleYearTargetSummary(@RequestParam("year") Integer year,
                                                   @RequestParam("type") Integer type,
                                                   @RequestParam("user_id") Long user_id) {
        return new CommonResult().success(type != 3 ? saleYearTargetService.querySaleYearTargetSummary(year, type, user_id) : saleYearTargetService.querySaleAllYearTargetSummary(year, user_id));
    }


    @GetMapping("/queryThisMonthTarget")
    @ApiOperation("根据type查询本月目标数据")
    public CommonResult queryThisMonthTargetData(@RequestParam("type") Integer type) {
        return new CommonResult().success(service.queryThisMonthTargetData(type));
    }

    @PostMapping("/addSigningTime")
    @ApiOperation("添加签约时间")
    public CommonResult addOrEditSigningTime(@RequestBody SigningTimeDTO dto) {
        return new CommonResult().success(service.addOrEditSigningTime(dto));
    }

    @PostMapping("/saveMonthCustomerTarget")
    @ApiOperation("保存月度的客户目标")
    public CommonResult saveThisMonthCustomerTarget(@RequestBody List<FillWeekCustomerTargetDTO> dtoList) {
        return new CommonResult().success(service.saveThisMonthCustomerTarget(dtoList));
    }

    @PostMapping("/addVisitRecord")
    @ApiOperation("添加跟进记录")
    public CommonResult addVisitRecord(@RequestBody VisitRecordDTO dto) {
        return new CommonResult().success(service.addVisitRecord(dto));
    }

    @GetMapping("/signatureHistoryList")
    @ApiOperation("签约历史列表")
    public ApiResult<Page<SignatureHistoryVO>> signatureHistoryList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                    @RequestParam(value = "page_size", defaultValue = "10") Integer page_size,
                                                                    @RequestParam("customer_id") Long customer_id,
                                                                    @RequestParam(value = "begin_date", required = false) @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin_date,
                                                                    @RequestParam(value = "end_date", required = false) @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end_date) {
        return ApiResult.ok(service.signatureHistoryList(page, page_size, customer_id, begin_date, end_date));
    }

    @GetMapping("/nextMonthTarget")
    @ApiOperation("下个月的数据")
    public CommonResult nextMonthTarget(@RequestParam("type") Integer type) {
        return new CommonResult().success(service.nextMonthTarget(type));
    }

    @PostMapping("/confirmReceive")
    @ApiOperation("确认收到")
    public CommonResult confirmReceive(@RequestParam("id") Long id) {
        return new CommonResult().success(service.confirmReceive(id));
    }

    @GetMapping("/yearWeek/list")
    @ApiOperation("根据客户获取周期列表")
    public CommonResult list(@RequestParam("customer_id") Long customer_id, @RequestParam(value = "record_id", required = false) Long record_id) {
        return new CommonResult().success(yearWeekService.selectList(customer_id, record_id));
    }


    @GetMapping("/queryCompanyMonthByMonth")
    @ApiOperation("查询月目标数据")
    public CommonResult queryCompanyMonthByMonth(@RequestParam("year") Integer year, @RequestParam("month") Integer month) {
        return new CommonResult().success(companyMonthService.queryCompanyMonthByMonth(year, month));
    }

    @GetMapping("/queryCompanyMonthPlanByMonth")
    @ApiOperation("查询月计划")
    public CommonResult queryCompanyMonthPlanByMonth(@RequestParam("year") Integer year, @RequestParam("month") Integer month) {
        return new CommonResult().success(companyMonthService.queryCompanyMonthPlanByMonth(year, month));
    }

    @PostMapping("/fillReasonsForNotCooperating")
    @ApiOperation("填写未成交原因")
    public CommonResult fillReasonsForNotCooperating(@RequestBody List<FillReasonsForNotCooperatingDTO> dtoList) {
        return new CommonResult().success(companyMonthService.fillReasonsForNotCooperating(dtoList));
    }

    @PostMapping("/fillAnnotation")
    @ApiOperation("填写批注")
    public CommonResult fillAnnotation(@RequestParam("customer_id") Long customer_id, @RequestParam("comments") String comments) {
        return new CommonResult().success(companyMonthService.fillAnnotation(customer_id, comments));
    }

    @ApiOperation("查询公司年度目标")
    @GetMapping("/queryCompanyYearTargetSummary")
    public ApiResult<CompanyYearTargetSummaryVO> queryCompanyYearTargetSummary(Integer year, Integer type) {
        return ApiResult.ok(companyYearService.queryCompanyYearTargetSummary(year, type));
    }
}
